package com.shinbada.modules.sys.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.shinbada.config.properties.ShinProperites;
import com.shinbada.config.swagger.IgnoreSwaggerParameter;
import com.shinbada.core.persistence.DataEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import java.util.List;

/**
 * 角色Entity
 *
 * @author initcap
 * @version 2016-12-05
 */
@Data
@ToString
@ApiModel("角色实体")
public class Role extends DataEntity<Role> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("角色名称")
    @Length(min = 1, max = 100)
    private String name;
    @ApiModelProperty("原角色名称")
    private String oldName;
    @ApiModelProperty("是否是系统数据")
    private String sysData;
    @ApiModelProperty("是否可用")
    private String useable;

    @IgnoreSwaggerParameter
    @ApiModelProperty("根据用户ID查询角色列表")
    private User user;

    @ApiModelProperty("拥有菜单列表")
    @IgnoreSwaggerParameter
    private List<Menu> menuList = Lists.newArrayList();
    @ApiModelProperty("数据范围")
    @IgnoreSwaggerParameter
    private List<DataRule> dataRuleList = Lists.newArrayList();


    public Role() {
        super();
        this.useable = ShinProperites.YES;
    }

    public Role(String id) {
        super(id);
    }

    public Role(User user) {
        this();
        this.user = user;
    }

    @JsonIgnore
    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }

    @JsonIgnore
    public List<String> getMenuIdList() {
        List<String> menuIdList = Lists.newArrayList();
        for (Menu menu : menuList) {
            menuIdList.add(menu.getId());
        }
        return menuIdList;
    }

    public void setMenuIdList(List<String> menuIdList) {
        menuList = Lists.newArrayList();
        for (String menuId : menuIdList) {
            Menu menu = new Menu();
            menu.setId(menuId);
            menuList.add(menu);
        }
    }

    public String getMenuIds() {
        return StringUtils.join(getMenuIdList(), ",");
    }

    public void setMenuIds(String menuIds) {
        menuList = Lists.newArrayList();
        if (menuIds != null) {
            String[] ids = StringUtils.split(menuIds, ",");
            setMenuIdList(Lists.newArrayList(ids));
        }
    }


    /**
     * 获取权限字符串列表
     */
    public List<String> getPermissions() {
        List<String> permissions = Lists.newArrayList();
        for (Menu menu : menuList) {
            if (menu.getPermission() != null && !"".equals(menu.getPermission())) {
                permissions.add(menu.getPermission());
            }
        }
        return permissions;
    }

    @JsonIgnore
    public List<DataRule> getDataRuleList() {
        return dataRuleList;
    }

    public void setDataRuleList(List<DataRule> dataRuleList) {
        this.dataRuleList = dataRuleList;
    }

    @JsonIgnore
    public List<String> getDataRuleIdList() {
        List<String> dataRuleIdList = Lists.newArrayList();
        for (DataRule dataRule : dataRuleList) {
            dataRuleIdList.add(dataRule.getId());
        }
        return dataRuleIdList;
    }

    public void setDataRuleIdList(List<String> dataRuleIdList) {
        dataRuleList = Lists.newArrayList();
        for (String dataRuleId : dataRuleIdList) {
            DataRule dataRule = new DataRule();
            dataRule.setId(dataRuleId);
            dataRuleList.add(dataRule);
        }
    }

    public String getDataRuleIds() {
        return StringUtils.join(getDataRuleIdList(), ",");
    }

    public void setDataRuleIds(String dataRuleIds) {
        dataRuleList = Lists.newArrayList();
        if (dataRuleIds != null) {
            String[] ids = StringUtils.split(dataRuleIds, ",");
            setDataRuleIdList(Lists.newArrayList(ids));
        }
    }

}
